package com.start.common.dataImport;

import com.baomidou.mybatisplus.extension.service.IService;
import com.start.common.tools.SpringContextTools;

import java.util.List;

public class DataExchangeExecute {

    private static DataExchangeExecute execute = new DataExchangeExecute();

    private DataExchangeExecute() {
        if (execute != null) throw new RuntimeException("该对象不能被多次实例化");
    }

    public static DataExchangeExecute getExecute() {
        return execute;
    }

    public void execute(DataExchangeEntity entity) throws ClassNotFoundException {
        final Class<?> aClass = Class.forName(entity.getEntityClass());
        final IService service = (IService) SpringContextTools.getBean(entity.getServiceClass());
        for (int i = 0; i < entity.getFilePaths().size(); i++) {
            String path = entity.getFilePaths().get(i);
            final List<?> list = ImportExportUtils.importExcelFile(aClass, path);
            entity.setFileIndex(i + 1);
        }
    }

    private void saveData(IService service, List<?> list) {
        if (list == null || list.size() < 1) {
            return;
        }
        service.saveBatch(list);
    }

}

